package com.nanyang.tms.service.pick.impl;

import cn.hutool.core.date.DateUtil;
import com.nanyang.common.core.annotation.SystemLog;
import com.nanyang.common.core.dict.CommonEnum;
import com.nanyang.common.core.domain.ApiResult;
import com.nanyang.common.core.exception.ExceptionProcessorUtils;
import com.nanyang.common.core.page.PageBean;
import com.nanyang.common.core.page.PageTableBean;
import com.nanyang.common.tool.utils.convert.BeanUtils;
import com.nanyang.tms.api.dto.request.pick.*;
import com.nanyang.tms.api.dto.response.pick.CompanyPickGoodsPageResponse;
import com.nanyang.tms.api.dto.response.pick.PickGoodsPageResponse;
import com.nanyang.tms.api.service.pick.PickGoodsRpcService;
import com.nanyang.tms.dao.pick.PickGoodsDao;
import com.nanyang.tms.domain.pick.PickGoodsDO;
import com.nanyang.tms.domain.pick.PickGoodsQuery;
import com.nanyang.tms.domain.pick.PickGoodsVO;
import com.nanyang.tms.manager.pick.PickGoodsManager;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

@Slf4j
@Service(version = "1.0.0", timeout = 10000, retries = 0, validation = "true")
public class PickGoodsRpcServiceImpl implements PickGoodsRpcService {
    @Autowired
    private PickGoodsManager pickGoodsManager;
    @Autowired
    private PickGoodsDao pickGoodsDao;

    @Override
    public ApiResult<PageBean<PickGoodsPageResponse>> listPickGoods(PickGoodsQueryRequest param) {
        ApiResult<PageBean<PickGoodsPageResponse>> apiResult = new ApiResult<>();
        try {
            param.requestCheck();
            PickGoodsQuery query = new PickGoodsQuery();
            BeanUtils.copyProperties(param, query);
            PageBean<PickGoodsVO> pickGoodsList = pickGoodsManager.listPickGoods(query);
            PageBean<PickGoodsPageResponse> resultList = new PageTableBean<>(query.getPageIndex(), query.getPageSize());
            resultList.setTotalItem(pickGoodsList.getTotalItem());
            BeanUtils.copyListProperties(pickGoodsList.getData(), resultList.getData(), PickGoodsPageResponse.class);
            apiResult.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            apiResult.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            apiResult.setData(resultList);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(apiResult, e);
        }
        return apiResult;
    }
    @SystemLog(module =SystemLog.Module.COMPANY,template="pickDriverUpdate")
    @Override
    public ApiResult<Boolean> updateDriver(PickGoodsDriverRequest param) {
        ApiResult<Boolean> result = new ApiResult<>();
        try {
            param.requestCheck();
            PickGoodsDO pickGoodsDO = new  PickGoodsDO();
            BeanUtils.copyProperties(param,pickGoodsDO);
            pickGoodsDO.setGmtModifiedPin(param.getPin());
            pickGoodsDao.updateDriverState(pickGoodsDO);
            result.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            result.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            result.setData(true);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(result, e);
        }
        return result;
    }
    @SystemLog(module =SystemLog.Module.COMPANY,template="pickDriver")
    @Override
    public ApiResult<Boolean> driverState(PickGoodsDriverRequest param) {
        ApiResult<Boolean> result = new ApiResult<>();
        try {
            param.requestCheck();
            PickGoodsDO pickGoodsDO = new  PickGoodsDO();
            BeanUtils.copyProperties(param,pickGoodsDO);
            pickGoodsDO.setGmtModifiedPin(param.getPin());
            pickGoodsDao.driverState(pickGoodsDO);
            result.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            result.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            result.setData(true);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(result, e);
        }
        return result;
    }
    @SystemLog(module =SystemLog.Module.COMPANY,template="pickCancel")
    @Override
    public ApiResult<Boolean> cancelState(PickGoodsUpdateStateRequest param) {
        ApiResult<Boolean> result = new ApiResult<>();
        try {
            param.requestCheck();
            PickGoodsDO pickGoodsDO = new  PickGoodsDO();
            BeanUtils.copyProperties(param,pickGoodsDO);
            pickGoodsDO.setGmtModifiedPin(param.getPin());
            pickGoodsDao.cancelState(pickGoodsDO);
            result.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            result.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            result.setData(true);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(result, e);
        }
        return result;
    }
    @SystemLog(module =SystemLog.Module.COMPANY,template="pickAlready")
    @Override
    public ApiResult<Boolean> alreadyState(PickGoodsUpdateStateRequest param) {
        ApiResult<Boolean> result = new ApiResult<>();
        try {
            param.requestCheck();
            PickGoodsDO pickGoodsDO = new  PickGoodsDO();
            BeanUtils.copyProperties(param,pickGoodsDO);
            pickGoodsDO.setGmtModifiedPin(param.getPin());
            pickGoodsDao.alreadyState(pickGoodsDO);
            result.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            result.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            result.setData(true);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(result, e);
        }
        return result;
    }
    @Override
    public ApiResult<Boolean> savePickGoods(PickGoodsAddRequest param) {
        ApiResult<Boolean> result = new ApiResult<>();
        try {
            param.requestCheck();
            PickGoodsVO pickGoodsVO = new  PickGoodsVO();
            BeanUtils.copyProperties(param,pickGoodsVO);
            pickGoodsVO.setGmtModifiedPin(param.getPin());
            pickGoodsVO.setPickTime(DateUtil.parseDateTime(param.getPickTime()));
            pickGoodsManager.savePickGoods(pickGoodsVO);
            result.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            result.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            result.setData(true);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(result, e);
        }
        return result;
    }

    @Override
    public ApiResult<PageBean<CompanyPickGoodsPageResponse>> listCompanyPickGoods(CompanyPickGoodsQueryRequest param) {
        ApiResult<PageBean<CompanyPickGoodsPageResponse>> apiResult = new ApiResult<>();
        try {
            param.requestCheck();
            PickGoodsQuery query = new PickGoodsQuery();
            BeanUtils.copyProperties(param, query);
            PageBean<PickGoodsVO> pickGoodsList = pickGoodsManager.listPickGoods(query);
            PageBean<CompanyPickGoodsPageResponse> resultList = new PageTableBean<>(query.getPageIndex(), query.getPageSize());
            resultList.setTotalItem(pickGoodsList.getTotalItem());
            BeanUtils.copyListProperties(pickGoodsList.getData(), resultList.getData(), CompanyPickGoodsPageResponse.class);
            apiResult.setCode(CommonEnum.REQUEST_SUCCESS.getCode());
            apiResult.setMessage(CommonEnum.REQUEST_SUCCESS.getMessage());
            apiResult.setData(resultList);
        } catch (Exception e) {
            log.error(CommonEnum.SYSTEM_EXCEPTION.getMessage(), e);
            ExceptionProcessorUtils.wrapperHandlerException(apiResult, e);
        }
        return apiResult;
    }
}
